// Avoid `console` errors in browsers that lack a console.
(function() {
    var method;
    var noop = function() {};
    var methods = [
        'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
        'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
        'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
        'timeStamp', 'trace', 'warn'
    ];
    var length = methods.length;
    var console = (window.console = window.console || {});

    while (length--) {
        method = methods[length];

        // Only stub undefined methods.
        if (!console[method]) {
            console[method] = noop;
        }
    }
}());
(function() {
    var dispatchEvent = window.dispatchEvent;
    /*
     * 基础配置
     */
    window.server_url = (location.host == "dev.dotnar.com:2222" || location.host.indexOf("192.168.") == 0) ? "http://" + location.hostname + ":3000/" : "http://api.dotnar.com/";

    var requireConfig = {
        // baseUrl: "./",
        paths: {
            //应用程序核心
            "jSouper_base": (location.host === "dev.dotnar.com:2222") ? ["http://localhost:9000/build/jSouper", "./js/lib/jSouper"] : "./js/lib/jSouper.min",
            "jSouper": "./js/lib/jsouper.handler",
            "tools": "./js/lib/tools",
            "appConfig": window.server_url + "./config/config.json?_=" + Math.random(),
            // "jSouper":"./js/lib/jSouper",
            //交互层核心
            "jQuery": "./js/lib/jquery-1.11.1.min",
            "jQuery.widget": "./js/lib/jquery.widget.min",
            "jQuery.easing": "./js/lib/jquery.easing.1.3.min",
            "jQuery.mousewheel": "./js/lib/jquery.mousewheel",
            "jQuery.colorPicker": "./js/lib/colorpicker/js/colorpicker",

            //弹出框插件
            "jQuery.notify": "./js/lib/jquery.notify",

            //二维码应用插件
            "jQuery.qrcode": "./js/lib/jquery.qrcode.min",

            //hash路由组件
            "routie": "./js/lib/routie.min",
            //Cookies操作库
            "Cookies": "./js/lib/cookies.min",

            "shim_json": "./js/lib/json3",
            "shim_html5": "./js/lib/html5",

            /*
             * RequireJs 插件，这里命名决定使用前缀，如"css" => "css!"
             */
            //requireJs CSS插件
            "r_css": "./js/lib/require.css2",
            //requireJs Text插件
            "r_text": "./js/lib/require.text",
            //requireJs 国际化插件
            "r_i18n": "./js/lib/require.i18n",

            /*
             * 通用组件包
             * 包括jSouper、jQuery、require.css"
             */
            "common": "./js/require.common",
            /*
             * 时间插件
             */
            "moment": "./js/lib/moment.min",
            "moment-locale-zh-cn": "./js/lib/moment.locale.zh-cn",
            /*
             * Socket 客户端
             */
            "SockJS": "./js/lib/sock",
            /*
             * 应用程序主体
             */
            "core": "./js/pages/_core"
        },
        shim: {
            "jSouper": {
                deps: ["jSouper_base"]
            },
            "jQuery.widget": {
                deps: ["jQuery", "jQuery.easing", "jQuery.mousewheel"]
            },
            "jQuery.easing": {
                deps: ["jQuery"]
            },
            "jQuery.mousewheel": {
                deps: ["jQuery"]
            },
            "jQuery.notify": {
                deps: ["jQuery"]
            },
            "jQuery.qrcode": {
                deps: ["jQuery"]
            },
            "jQuery.colorPicker": {
                deps: ["jQuery", "r_css!./js/lib/colorpicker/css/colorpicker"]
            },
            "metro": {
                deps: ["jQuery", "jQuery.widget", "metro-core"]
            },
            "moment-locale-zh-cn": {
                deps: ["moment"]
            }
        }
    };
    if (!dispatchEvent) {
        /*
         * 非现代浏览器的话则加入html5 history垫片
         */
        // requireConfig.shim.routie = {deps:["./js/lib/history.min.js"]};
        /*
         * 不支持JSON的浏览器加入JSON3包的支持
         */
        requireConfig.shim.SockJS = {
            deps: ["shim_json"]
        };
    }

    // /*
    //  * 额外jQuery插件
    //  */
    // var jQueryPlugs = {
    //     "select2": [
    //         "r_css!./UI_mutual/select2/select2.css",
    //         "./UI_mutual/select2/select2"
    //     ]
    // };
    // for (var jquery_p in jQueryPlugs) {
    //     (function(jquery_p) {
    //         console.info("配置并定义" + jquery_p + "插件");
    //         var _shim = jQueryPlugs[jquery_p].slice();
    //         _shim.unshift("jQuery");
    //         define(jquery_p, _shim, function(jQuery) {
    //             console.log(arguments, _shim);
    //             return jQuery;
    //         });
    //     }(jquery_p));
    // }
    /*
     * jSouper的模板文件 ==> UI组件
     * 使用html后缀确保编辑器能自动高亮
     */
    var jSouperTemplates = {
        "UI.Form": [
            "r_text!./template/form.html",
            "r_text!./template/form-input.html",
            "r_css!./css/jSouper-icons.css",
            "r_css!./css/metro-calendar.css"
        ],
        "UI.Input": [
            "r_css!./template/css/xmp.default.css",
            "r_css!./template/css/form.css"
        ],
        "UI.Model": [
            "r_text!./template/xmp.model.html",
            "r_text!./template/xmp.model.tag.html",
            "r_css!./css/icon.css"
        ],
        "UI.testing": [
            "r_css!./css/model.css",
            "r_css!./css/default.css",
            "r_css!./css/icon.css",
            "r_text!./template/xmp.testing.html"
        ],
        "UI.jSouper": [
            "./template/jsouper-ui/jsouper.base-ui"
        ],
        "UI.Tag": [
            "r_text!./template/xmp.customTag.html",
            "r_css!./template/css/xmp.customTag.css"
        ],
        "UI.Comic": [
            "./js/tools/top_bar_keep"
        ]
    };
    var tpl_shims;
    for (var templateName in jSouperTemplates) {
        //生成requireJS的依赖关系数组
        tpl_shims = jSouperTemplates[templateName].slice();
        tpl_shims.unshift("jSouper");
        //定义require的UI模块
        define(templateName, tpl_shims, function(jSouper) {
            var args = Array.prototype.slice.call(arguments);
            args.shift();
            //解析所有模板文件
            for (var i = 0, len = args.length; i < len; i += 1) {
                args[i] && jSouper.parse(args[i]);
            };
            //手动触发更新页面
            jSouper.App && jSouper.App.model.touchOff(".");
        });
    }

    /*
     * 完成配置
     */
    define("require.config", requireConfig)
    require.config(requireConfig);

}());